package com.extra_archive.framework.utils 
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.geom.Matrix;
	import flash.geom.Point;

	/**
	 * DisplayObjectUtil
	 * @author implict (http://extra-archive.com)
	 *
	 * date : 2009. 08. 22
	 * description :
	 *
	 */ 
	public class DisplayObjectUtils 
	{
		public static function removeAllChildren(container: DisplayObjectContainer): void
		{
			var count: int = container.numChildren;
			
			for (var i: int; i < count; i++)
			{
				container.removeChildAt(0);
			}
		}
		
		/**
		 * transform displayObject
		 */
		public static function flipHorizontal(target: DisplayObject): void
		{
			var matrix:Matrix = target.transform.matrix;
			matrix.transformPoint(new Point(target.width/2, target.height/2));
			if(matrix.a>0){
				matrix.a=-1*matrix.a;
				matrix.tx=target.width + target.x;
			}
			else
			{
				matrix.a=-1*matrix.a;
				matrix.tx=target.x-target.width;
			}
			target.transform.matrix=matrix;
		}
		
		public static function flipVertical(target: DisplayObject): void
		{
			var matrix:Matrix = target.transform.matrix;
			matrix.transformPoint(new Point(target.width/2,target.height/2));
			if(matrix.d>0){
				matrix.d=-1*matrix.d;
				matrix.ty=target.y+target.height;
			}
			else
			{
				matrix.d=-1*matrix.d;
				matrix.ty=target.y-target.height;
			}
			target.transform.matrix=matrix;
		}
		
		public static function drawRectShape(width: uint, height: uint, color: Number = 0, alpha: Number = 1): Shape
	 	{
	    	var shape: Shape = new Shape();
	    	
	    	shape.graphics.beginFill(color);
	    	shape.graphics.drawRect(0, 0, width, height);
	    	shape.graphics.endFill();
	    	shape.alpha = alpha;
	    	
	    	return  shape;
	    }
	    
	    /**
	     * draw displayObejct 
	     */    
	    public static function drawCircleShape(radius: Number, color: Number = 0, alpha: Number = 1): Shape
	 	{
	    	var shape: Shape = new Shape();
	    	
	    	shape.graphics.beginFill(color);
	    	shape.graphics.drawCircle(0,0,radius);
	    	shape.graphics.endFill();
	    	shape.alpha = alpha;
	    	
	    	return  shape;
	    }
	    
	    public static function drawRectSprite(width: uint, height: uint, color: Number = 0, alpha: Number = 1): Sprite
	    {
	    	var sprite: Sprite = new Sprite();
	    	
	    	sprite.graphics.beginFill(color);
	    	sprite.graphics.drawRect(0, 0, width, height);
	    	sprite.graphics.endFill();
	    	sprite.alpha = alpha;
	    	
	    	return  sprite;
	    }
	    
	    public static function drawCircleSprite(radius: Number, color: Number = 0, alpha: Number = 1): Sprite
	    {
	    	var sprite: Sprite = new Sprite();
	    	
	    	sprite.graphics.beginFill(color);
	    	sprite.graphics.drawCircle(0,0,radius);
	    	sprite.graphics.endFill();
	    	sprite.alpha = alpha;
	    	
	    	return  sprite;
	    }
	}
}
